/**
 * 
 */
package com.dingding.order.core.orderlist.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.common.core.util.GetDistanceUtil;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.orderlist.dao.ITaxiGetNearbyOrderListDao;
import com.dingding.order.dto.orderlist.NearbyOrderListSQL;
import com.dingding.order.dto.orderlist.ReqTaxiGetNearbyOrderListDTO;
import com.dingding.order.dto.orderlist.TaxiComparatorUtil;
import com.dingding.order.dto.orderlist.TaxiNearbyOrder;

/**
 * @ClassName: TaxiGetNearbyOrderListDaoImpl
 * @Description:出租车获取附近订单列表dao
 * @author zhouxuan
 * @date 2016年6月30日 下午2:13:28
 */
@Component("taxiGetNearbyOrderListDao")
public class TaxiGetNearbyOrderListDaoImpl extends BaseDaoImpl<UserTaxiOrdert> implements ITaxiGetNearbyOrderListDao {
	/**
	 * 
	 * @Title: taxiGetNearbyOrderList
	 * @Description:出租车获取附近订单列表
	 * @author zhouxuan
	 * @return List<TaxiNearbyOrder> 排序后的出租车订单列表
	 * @since V2.3.0
	 */
	@Transactional(readOnly = true)
	public List<TaxiNearbyOrder> taxiGetNearbyOrderList(ReqTaxiGetNearbyOrderListDTO reqTaxiGetNearbyOrderListDTO) {
		// 创建用于存储获取附近列表的对象
		List<Object[]> objects = null;
		// 用于截取所在城市最后一个字带市或者不带市的情况
		if (reqTaxiGetNearbyOrderListDTO.getPayCity().substring(reqTaxiGetNearbyOrderListDTO.getPayCity().length() - 1,
				reqTaxiGetNearbyOrderListDTO.getPayCity().length()).equals("市")) {
			reqTaxiGetNearbyOrderListDTO.setPayCity(reqTaxiGetNearbyOrderListDTO.getPayCity().substring(0,
					reqTaxiGetNearbyOrderListDTO.getPayCity().length() - 1));
		}
		try {
			objects = super.sqlQueryListObj(NearbyOrderListSQL.TaxiGetNearbyOrderList(), false,
					reqTaxiGetNearbyOrderListDTO.getUserId(), reqTaxiGetNearbyOrderListDTO.getPayCity() + "%",
					(reqTaxiGetNearbyOrderListDTO.getPageIndex() - 1) * reqTaxiGetNearbyOrderListDTO.getPageSize(),
					reqTaxiGetNearbyOrderListDTO.getPageSize());
		} catch (Exception e) {
			log.error("出租车获取附近订单出现异常！类 taxiGetNearbyOrderList()", e);
		}
		// 创建出租车附近订单列表
		List<TaxiNearbyOrder> taxiNearbyOrderLists = null;

		// 过滤附近订单列表
		if (objects != null && objects.size() > 0) {
			taxiNearbyOrderLists = this.filterNearbyOrderLists(objects, reqTaxiGetNearbyOrderListDTO);
		}

		return taxiNearbyOrderLists;
	}

	/**
	 * 
	 * @Title: taxiGetNearbyOrderLists
	 * @Description:过滤出租车附近订单列表
	 * @author zhouxuan
	 * @return List<TaxiNearbyOrder> 过滤不符合条件过后的出租车订单列表
	 * @since V2.3.0
	 */
	private List<TaxiNearbyOrder> filterNearbyOrderLists(List<Object[]> objects,
			ReqTaxiGetNearbyOrderListDTO reqTaxiGetNearbyOrderListDTO) {
		// 创建要响应给客户端的订单集合
		List<TaxiNearbyOrder> lists = new ArrayList<TaxiNearbyOrder>();
		// 获取当前用户订单
		for (Object[] obj : objects) {
			// 判断要使用的字段是否为空
			boolean flag = getOrderInfoListValidate(obj, reqTaxiGetNearbyOrderListDTO);

			if (!flag) {
				log.info("当前订单不符合条件");
				continue;
			}
			// 创建临时用于存储订单的对象
			TaxiNearbyOrder taxiNearbyOrder = new TaxiNearbyOrder();

			// 数据转换
			dataChange(obj, taxiNearbyOrder);
			// 设置当前位置与订单起始地的距离
			taxiNearbyOrder.setStartDistance(getCurrentDistance(taxiNearbyOrder.getStartLongitude(),
					taxiNearbyOrder.getStartLatitude(), reqTaxiGetNearbyOrderListDTO.getCurrentLongitude(),
					reqTaxiGetNearbyOrderListDTO.getCurrentLatitude()));

			// 添加到集合
			lists.add(taxiNearbyOrder);
		}
		// 根据距离远近排序
		Collections.sort(lists, new TaxiComparatorUtil());
		return lists;
	}

	/**
	 * @Title: getOrderInfoListValidate
	 * @Description:判断要使用到的字段是否为空
	 * @author zhouxuan
	 * @return boolean true为符合要求，false为不符合要求
	 * @since V2.3.0
	 */
	private boolean getOrderInfoListValidate(Object[] obj, ReqTaxiGetNearbyOrderListDTO reqTaxiGetNearbyOrderListDTO) {
		// 终点纬度
		if (obj[2] == null) {
			return false;
		}
		// 终点经度
		if (obj[3] == null) {
			return false;
		}
		// 起点纬度
		if (obj[13] == null) {
			return false;
		}
		// 起点经度
		if (obj[14] == null) {
			return false;
		}
		return true;
	}

	/**
	 * 
	 * @Title: dataChange
	 * @Description:数据转换
	 * @author zhouxuan
	 * @since V2.3.0
	 */
	private void dataChange(Object[] obj, TaxiNearbyOrder taxiNearbyOrder) {
		// 订单里程
		if (obj[0] != null) {
			taxiNearbyOrder.setDistance(ToolUtil.castDouble(obj[0]));
		}
		// 目的地
		if (obj[1] != null) {
			taxiNearbyOrder.setEndAddress(obj[1].toString());
		}
		// 终点纬度
		if (obj[2] != null) {
			taxiNearbyOrder.setEndLatitude(ToolUtil.castDouble(obj[2]));
		}
		// 终点经度
		if (obj[3] != null) {
			taxiNearbyOrder.setEndLongitude(ToolUtil.castDouble(obj[3]));
		}
		// 留言
		if (obj[4] != null) {
			taxiNearbyOrder.setMessage(obj[4].toString());
		}
		// 用户名称
		if (obj[5] != null) {
			taxiNearbyOrder.setNickName(obj[5].toString());
		}
		// 人数
		if (obj[6] != null) {
			taxiNearbyOrder.setNumber(ToolUtil.castInteger(obj[6]));
		}
		// 订单Id
		if (obj[7] != null) {
			taxiNearbyOrder.setOid(obj[7].toString());
		}
		// 订单状态
		if (obj[8] != null) {
			taxiNearbyOrder.setOrderStatus(ToolUtil.castInteger(obj[8]));
		}
		// 对方ID
		if (obj[9] != null) {
			taxiNearbyOrder.setOtherUserId(obj[9].toString());
		}
		// 出发时间
		String startTime = null;
		if (obj[10] != null) {
			startTime = DateFormatUtil.getMMddEHHss(obj[10]);
			taxiNearbyOrder.setSetOutTime(startTime);
			taxiNearbyOrder.setOutTime((Date) (obj[10]));
		}
		// 性别
		if (obj[11] != null) {
			taxiNearbyOrder.setSex(ToolUtil.castInteger(obj[11]));
		}
		// 出发地
		if (obj[12] != null) {
			taxiNearbyOrder.setStartAddress(obj[12].toString());
		}
		// 起点纬度
		if (obj[13] != null) {
			taxiNearbyOrder.setStartLatitude(ToolUtil.castDouble(obj[13]));
		}
		// 起点经度
		if (obj[14] != null) {
			taxiNearbyOrder.setStartLongitude(ToolUtil.castDouble(obj[14]));
		}
		// 手机号
		if (obj[15] != null) {
			taxiNearbyOrder.setTelePhone(obj[15].toString());
		}
		// 用户头像
		if (obj[16] != null) {
			taxiNearbyOrder.setUserAvatar(ToolUtil.imageAddress(obj[16].toString()));
		}
		// 头像地址
		if (obj[17] != null) {
			taxiNearbyOrder.setUserTabloidAvatar(ToolUtil.imageAddress(obj[17].toString()));
		}
		taxiNearbyOrder.setTitle(startTime + " 约" + taxiNearbyOrder.getDistance() + "公里");
		taxiNearbyOrder.setPublishType(2);//出租车
	}

	/**
	 * @Title: getCurrentDistance
	 * @Description: 获取当前用户距离订单起点距离
	 * @author zhouxuan
	 * @return double 返回当前位置到订单起点的距离
	 * @since V2.3.0
	 */
	private double getCurrentDistance(double startLongitude, double startLatitude, double currentLongitude,
			double currentLatitude) {

		// 计算用户当前位置距离订单起点位置的距离
		BigDecimal currentDistance = GetDistanceUtil.GetDistance(currentLongitude, currentLatitude, startLongitude,
				startLatitude);
		// 保留 一位小数
		return currentDistance.setScale(1,BigDecimal.ROUND_UP).doubleValue();
	}
}
